/**   
 * 文件名：PostService.java</br>
 * 描述： </br>
 * 开发人员：汪浩博 </br>
 * 创建时间： 2016-5-5
 */ 

package com.post.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.base.util.BeanUtil;
import com.base.util.Page;
import com.base.util.DateUtil;
import com.post.bean.Carousel;
import com.post.bean.Post;
import com.post.bean.PostVO;
import com.post.dao.PostDAO;
import com.topic.bean.Topic;
import com.user.bean.BBSUser;

/** 
 * 类名: PostService</br> 
 * 包名：com.post.service </br> 
 * 描述: </br>
 * 发布版本号：</br>
 * 开发人员： 汪浩博</br>
 * 创建时间： 2016-5-5 
 */

public class PostService {
	
	private PostDAO postDAO;

	public PostDAO getPostDAO() {
		return postDAO;
	}

	public void setPostDAO(PostDAO postDAO) {
		this.postDAO = postDAO;
	}
	/**
	 * 方法名: Querypage</br>
	 * 详述: 分页查询</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-5</br>
	 * @param post
	 * @param pagenum
	 * @param pagesize
	 * @param date 按日期筛选
	 * @param sort 按某字段排序
	 * @return
	 */
	public Page Querypage(PostVO postVo, int pagenum, int pagesize, String date, String sort, boolean back){
		String queryString = this.Sethql(postVo,date,sort,back);
		PostVO postVO = new PostVO();
		Page page = null;
		List<PostVO> list = new ArrayList<PostVO>();
		page = postDAO.getPage(queryString, pagenum, pagesize);
		Iterator<Object[]> iterator = page.getQueryResult().iterator();
		while(iterator.hasNext()){
			Object[] next = iterator.next();
			list.add(postVO.initPostVO(next));
		}
		page.setQueryResult(list);
		return page;
	}

	/**
	 * 方法名: Sethql</br>
	 * 详述: 注入hql</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-5</br>
	 * @param post
	 * @return
	 */
	public String Sethql(PostVO postVo, String date, String sort, boolean back){
		if(date == null){

		}else{
			//获取date天前的日期
			postVo.setPostTime(DateUtil.getSystemTime(-Integer.parseInt(date)));
		}
		StringBuilder queryString = new StringBuilder("from Post a , BBSUser b, Topic c where 1=1 and a.userId=b.id and a.topicId=c.id ");
		if(postVo!=null){		
			if (postVo.getId() != null && !"".equals(postVo.getId())) {
				queryString.append(" and a.id = '").append(postVo.getId()).append("'");
			}
			if (postVo.getTitle() != null && !"".equals(postVo.getTitle())){
				queryString.append(" and a.title like '%").append(postVo.getTitle()).append("%'");
			}
			if (postVo.getTopicId() != null && !"".equals(postVo.getTopicId())) {
				queryString.append(" and a.topicId = '").append(postVo.getTopicId()).append("'");
			}
			if (postVo.getPostTime() != null && !"".equals(postVo.getPostTime())) {
				queryString.append(" and a.postTime > '").append(postVo.getPostTime()).append("'");
			}
			if (postVo.getPostStatus() != null && !"".equals(postVo.getPostStatus())) {
				queryString.append(" and a.postStatus = '").append(postVo.getPostStatus()).append("'");
			}
			if (postVo.getNickName()!=null && !"".equals(postVo.getNickName())) {
				queryString.append(" and b.nickName like '%").append(postVo.getNickName()).append("%'");
			}
			if (postVo.getTopicName()!=null && !"".equals(postVo.getTopicName())) {
				queryString.append(" and c.topicName like '%").append(postVo.getTopicName()).append("%'");
			}
			if (!back) {
				queryString.append(" and a.postStatus != '1' ");
			}
			if(sort == null){
				queryString.append(" order by a.postTime");
			}else{
				queryString.append(" order by a."+sort);
			}
		}
		
		return queryString.toString();
	}
	/**
	 * 方法名: addPost</br>
	 * 详述: 新增或者更新已经存在的帖子</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-19</br>
	 * @param post
	 */
	public void addPost(Post post){
		try {
			postDAO.saveOrUpdate(post);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 方法名: findPostByUid</br>
	 * 详述: 通过用户id查询相关帖子,分页的</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-20</br>
	 * @param uid 用户id
	 * @return
	 */
	public Page findPagePostByUid(Long uid, Integer pagenum, Integer pagesize){
		Page page = null;
		String queryString = "from Post where userId = '"+uid+"' and postStatus !='1' order by postTime";
		page = postDAO.getPage(queryString, pagenum, pagesize);
		return page;
	}
	/**
	 * 方法名: updateLastReplyTime</br>
	 * 详述: 刷新帖子最后回复时间</br>
	 * 
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-20</br>
	 * @param pid
	 */
	public void updateLastReplyTime(String pid){
		String time = DateUtil.getStringTime();
		String hql = "update Post set lastreplytime = '" + time + "' where id='"+pid+"'";
		try {
			postDAO.updateHql(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 方法名: findCollectPostByUid</br>
	 * 详述: 通过用户id查询相关收藏的帖子,分页的</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-21</br>
	 * @param uid
	 * @param pagenum
	 * @param pagesize
	 * @return
	 */
	public Page findCollectPostByUid(Long uid, Integer pagenum, Integer pagesize){
		Page page = null;
		String queryString = "from Post where id in (select b.pid from UserCollect b where b.userid = '"+uid+"') ";
		page = postDAO.getPage(queryString, pagenum, pagesize);
		return page;
	}
	/**
	 * 方法名: getCountByTopic</br>
	 * 详述: 获取主题下帖子数量</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-4</br>
	 */
	public Long getCountByTopic(long topicId){
		Long count = (long) 0;
		String hql = "select count(*) from Post where topicId = '"+topicId+"' and postStatus!='1'";
		try {
			count = (Long) postDAO.findByHql(hql).get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}
	/**
	 * 方法名: deletePost</br>
	 * 详述: 通过id删除帖子</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-5</br>
	 */
	public void deletePost(String id){
		String hql = "delete from Post where id='"+id+"'";
		try {
			postDAO.delete(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 方法名: updatePostStatus</br>
	 * 详述: 更新帖子状态</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-6</br>
	 */
	public void updatePostStatus(String status,String pid){
		String hql = "update Post set postStatus = '" + status + "' where id='"+pid+"'";
		try {
			postDAO.updateHql(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 方法名: getPostById</br>
	 * 详述: 通过id获取帖子对象</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-10</br>
	 * @param pid
	 * @return
	 */
	public Post getPostById(String pid){
		String hql = "from Post where id='"+pid+"'";
		Post post = new Post();
		try {
			post = (Post) postDAO.findByHql(hql).get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return post;
	}
	/**
	 * 方法名: updatePost</br>
	 * 详述: 更新整个post对象</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-10</br>
	 * @param post
	 */
	public void updatePost(Post post){
		try {
			postDAO.update(post);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 方法名: getAllCarousel</br>
	 * 详述: 获取所有轮播对象</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-13</br>
	 * @return
	 */
	public List<Carousel> getAllCarousel(){
		List<Carousel> list = null;
		String hql = "from Carousel";
		try {
			postDAO.findByHql(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public void addCarousel(Carousel carousel){
		try {
			postDAO.insert(carousel);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public Carousel getSingleCarousel(String id){
		String hql = "from Carousel where id='"+id+"'";
		Carousel carousel = null;
		try {
			carousel = (Carousel) postDAO.findByHql(hql).get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return carousel;
	}
	public void deleteCarousel(Carousel carousel){
		try {
			postDAO.delete(carousel);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
